package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Alarm;
import com.ruoyi.system.service.IAlarmService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 告警信息Controller
 * 
 * @author ruoyi
 * @date 2025-06-28
 */
@RestController
@RequestMapping("/system/alarm")
public class AlarmController extends BaseController
{
    @Autowired
    private IAlarmService alarmService;

    /**
     * 查询告警信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:alarm:list')")
    @GetMapping("/list")
    public TableDataInfo list(Alarm alarm)
    {
        startPage();
        List<Alarm> list = alarmService.selectAlarmList(alarm);
        return getDataTable(list);
    }

    /**
     * 导出告警信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:alarm:export')")
    @Log(title = "告警信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Alarm alarm)
    {
        List<Alarm> list = alarmService.selectAlarmList(alarm);
        ExcelUtil<Alarm> util = new ExcelUtil<Alarm>(Alarm.class);
        util.exportExcel(response, list, "告警信息数据");
    }

    /**
     * 获取告警信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:alarm:query')")
    @GetMapping(value = "/{aId}")
    public AjaxResult getInfo(@PathVariable("aId") Integer aId)
    {
        return success(alarmService.selectAlarmByAId(aId));
    }

    /**
     * 新增告警信息
     */
    @PreAuthorize("@ss.hasPermi('system:alarm:add')")
    @Log(title = "告警信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Alarm alarm)
    {
        return toAjax(alarmService.insertAlarm(alarm));
    }

    /**
     * 修改告警信息
     */
    @PreAuthorize("@ss.hasPermi('system:alarm:edit')")
    @Log(title = "告警信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Alarm alarm)
    {
        return toAjax(alarmService.updateAlarm(alarm));
    }

    /**
     * 删除告警信息
     */
    @PreAuthorize("@ss.hasPermi('system:alarm:remove')")
    @Log(title = "告警信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{aIds}")
    public AjaxResult remove(@PathVariable Integer[] aIds)
    {
        return toAjax(alarmService.deleteAlarmByAIds(aIds));
    }
    // 获取充电站下拉列表
@GetMapping("/stationList")
public AjaxResult getStationList() {
    return AjaxResult.success(alarmService.selectAllStations());
}

// 获取指定充电站的告警分析数据
@GetMapping("/analysis")
public AjaxResult getAlarmAnalysis(@RequestParam Integer stationId) {
    return AjaxResult.success(alarmService.selectAlarmAnalysisByStationId(stationId));
}
// 查询每日告警次数
@GetMapping("/dailyCount")
public AjaxResult getDailyAlarmCount(@RequestParam Integer stationId, @RequestParam String month) {
    return AjaxResult.success(alarmService.selectDailyAlarmCount(stationId, month));
}
// 查询每日告警时长占比
@GetMapping("/dailyDurationPercent") 
public AjaxResult getDailyAlarmDurationPercent(@RequestParam Integer stationId, @RequestParam String month) {
    return AjaxResult.success(alarmService.selectDailyAlarmDurationPercent(stationId, month));
}

}
